/**
 * @author Javen 
 * @copyright 2019-02-03 14:47:34 javendev@126.com 
 * @description 任何组合
 */


cc.Class({
    extends: cc.Component,

    properties: {
        nodes: {
            default: [],
            type: cc.Node
        },
        _orders: [],
    },

    // onLoad () {},

    start() {
        this._initEvent();
        this._orders.push(1, 2, 3, 4);

        console.log(this._orders, this.nodes);


        cc.director.on("homeImgTouchEnd", function (event) {
            console.log("回调:", event.node);
            let node = event.node;
            let firstPosition = event.firstPosition;

            let isTouch = false;
            for (let index = 0; index < this.nodes.length; index++) {
                let tempNode = this.nodes[index];
                if (tempNode.name != node.name) {
                    if (this.isTouch(tempNode, node, 150)) {
                        isTouch = true;
                        let moveNodeIndex = node.name.split("_")[1];
                        let tempNodeIndex = tempNode.name.split("_")[1];

                        //设置名称
                        node.name = node.name.split("_")[0] + "_" + moveNodeIndex;
                        tempNode.name = tempNode.name.split("_")[0] + "_" + tempNodeIndex;

                        //获取之前的位置
                        let moveOderIndex = this._orders.indexOf(parseInt(moveNodeIndex));
                        let tempOderIndex = this._orders.indexOf(parseInt(tempNodeIndex));

                        //交换位置
                        this._setPosition(node, tempOderIndex);
                        this._setPosition(tempNode, moveOderIndex);
                        //排序
                        this._orders[moveOderIndex] = parseInt(tempNodeIndex);
                        this._orders[tempOderIndex] = parseInt(moveNodeIndex);

                        console.log("结果:", this._orders, this.nodes);
                        break;
                    }
                }
            }

            if (!isTouch) {
                console.log("没有。。。。");

                node.position = firstPosition;
            }
        }.bind(this));
    },

    isTouch(nodeOne, nodeTwo, r) {
        let v1 = new cc.Vec2(nodeTwo.x, nodeTwo.y);
        let v2 = new cc.Vec2(nodeOne.x, nodeOne.y);
        if (v2.sub(v1).mag() <= r) {
            return true;
        } else {
            return false;
        }
    },
    _setPosition(node, number) {
        switch (number) {
            case 0:
                node.setPosition(-345, 0);
                break;
            case 1:
                node.setPosition(-115, 0);
                break;
            case 2:
                node.setPosition(115, 0);
                break;
            case 3:
                node.setPosition(345, 0);
                break;

            default:
                break;
        }
    },

    _initEvent() {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this._onKeyDown, this);
    },
    _onKeyDown(event) {
        switch (event.keyCode) {
            case cc.macro.KEY.enter:
                console.log("回车键...");
                break;
            default:
                break;
        }
    },
    getRandom(min, max) {
        return parseInt(Math.random() * (max - min + 1) + min, 10);
    },

    onDestroy() {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN, this._onKeyDown, this);
    },
    // update(dt) {},
});